package com.jy.wms.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsArtificialReplenishmentMapper;
import com.jy.wms.dao.WmsPurchaseOrderMapper;
import com.jy.wms.pojo.WmsArtificialReplenishment;
import com.jy.wms.pojo.WmsArtificialReplenishmentDetailed;
import com.jy.wms.dao.WmsArtificialReplenishmentDetailedMapper;
import com.jy.wms.pojo.WmsPurchaseOrder;
import com.jy.wms.pojo.WmsPurchaseOrderDetailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName: WmsArtificialReplenishmentDetailedService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Mon Nov 27 17:20:28 GMT+08:00 2017
 */

@Service
public class WmsArtificialReplenishmentDetailedService extends BaseService<WmsArtificialReplenishmentDetailedMapper, WmsArtificialReplenishmentDetailed> {

    @Autowired
    private WmsArtificialReplenishmentMapper wmsArtificialReplenishmentMapper;

    /**
     * 插入一条记录
     *
     * @param list 实体对象
     * @return int
     */
    public Integer baseInsert(List<WmsArtificialReplenishmentDetailed> list,String hzid,String ckid) throws ServiceException, SucessException {
        Integer a =0;
        for(WmsArtificialReplenishmentDetailed entity:list) {
            entity.setId(UUIDUtils.uuid());
            entity.setHzid(hzid);
            entity.setCkid(ckid);
             a = dao.baseInsert(entity);
        }


        if (a > 0) {
            throw new SucessException("补货明细增加成功");
        } else {
            throw new ServiceException("补货明细增加失败");
        }
    }

    /**
     * 插入一条记录（匹配有值的字段）
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsertSelective(List<WmsArtificialReplenishmentDetailed> entity) {
        return dao.baseInsertSelective(entity);
    }

    /**
     * 删除（根据主键ID删除）
     *
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey(String id) {
        return dao.baseDeleteByPrimaryKey(id);
    }

    /**
     * 删除（根据ID 批量删除）
     *
     * @param idList 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys(List<String> idList, String bhid) throws SucessException, ServiceException {
        WmsArtificialReplenishment wmsArtificialReplenishment = wmsArtificialReplenishmentMapper.baseSelectByPrimaryKey(bhid);//通过补货单id查询对象
        //查询当前采购订单下的明细数量，如果只剩下一条明细那么不可以删除
        Integer count = dao.selectCount(new EntityWrapper<WmsArtificialReplenishmentDetailed>().eq("bhid", bhid));
        //查询当前补货单下的明细数量，如果只剩下一条明细那么不可以删除
        Integer a = dao.selectCount(new EntityWrapper<WmsArtificialReplenishmentDetailed>().eq("bhid", bhid));
        if (a == 1) {
            throw new ServiceException("此补货单只包含一条明细，不可以删除");
        } else {
            if (wmsArtificialReplenishment.getZt().compareTo(BigDecimal.ZERO) != 0) {
                throw new ServiceException("只能删除补货单状态为“初始”的记录明细");
            } else {
                Integer b = dao.baseDeleteBathPrimaryKeys(idList);
                if (a > 0) {
                    throw new SucessException("发运明细删除成功");
                } else {
                    throw new ServiceException("发运明细删除失败");
                }
            }

        }

    }

    /**
     * 分页查询
     *
     * @param entity 实体
     * @return Page<WmsArtificialReplenishmentDetailed>
     */
    public Page<WmsArtificialReplenishmentDetailed> baseSelectPage(WmsArtificialReplenishmentDetailed entity) {
        PageHelper.startPage(entity.getPage(), entity.getRows());
        return (Page<WmsArtificialReplenishmentDetailed>) dao.baseSelectPage(entity);
    }

    /**
     * 查询（根据主键ID查询）
     *
     * @param id 主键ID
     * @return WmsArtificialReplenishmentDetailed
     */
    public WmsArtificialReplenishmentDetailed baseSelectByPrimaryKey(String id) {
        return dao.baseSelectByPrimaryKey(id);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<WmsArtificialReplenishmentDetailed> idList) {
        return dao.baseSelectBathPrimaryKeys(idList);
    }

    /**
     * 修改 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective(WmsArtificialReplenishmentDetailed entity) throws SucessException, ServiceException {
        Integer a = dao.baseUpdateByPrimaryKeySelective(entity);
        if (a > 0) {
            throw new SucessException("补货单明细修改成功");
        } else {
            throw new ServiceException("补货单明细修改失败");
        }

    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey(WmsArtificialReplenishmentDetailed entity) {
        return dao.baseUpdateByPrimaryKey(entity);
    }
}
